from datetime import datetime
from typing import List, Optional, Dict, Any
from pydantic import BaseModel


class FileUploadRequest(BaseModel):
    """文件上传请求"""
    knowledge_base: Optional[str] = None  # 知识库名称 (可选)


class FileUploadResponse(BaseModel):
    """文件上传响应"""
    code: int = 200
    message: str = "文件上传成功"
    data: Optional[Dict[str, Any]] = None


class FileUploadData(BaseModel):
    """文件上传数据"""
    file_id: str  # 文件唯一标识
    file_name: str  # 原始文件名
    file_size: int  # 文件大小(字节)
    file_path: str  # 服务器存储路径
    upload_time: datetime  # 上传时间
    status: str  # 文件状态: uploaded, processing, failed
    file_hash: str  # 文件哈希值(防重复)

    class Config:
        from_attributes = True


class UploadProgressResponse(BaseModel):
    """上传进度响应"""
    code: int = 200
    data: Optional[Dict[str, Any]] = None


class UploadProgressData(BaseModel):
    """上传进度数据"""
    file_id: str
    progress: int  # 上传进度 (0-100)
    status: str  # 状态: uploading, completed, failed
    uploaded_bytes: int  # 已上传字节数
    total_bytes: int  # 总字节数
    speed: int  # 上传速度 (字节/秒)
    estimated_time: float  # 预计剩余时间 (秒)

    class Config:
        from_attributes = True


class FileAnalysisRequest(BaseModel):
    """文件分析请求"""
    file_id: str  # 文件ID
    analysis_type: str = "document"  # 分析类型: document, code, security
    options: Optional[Dict[str, Any]] = None  # 分析选项


class FileAnalysisOptions(BaseModel):
    """文件分析选项"""
    extract_keywords: bool = True  # 提取关键词
    generate_summary: bool = True  # 生成摘要
    identify_structure: bool = True  # 识别文档结构


class AnalysisProgressEvent(BaseModel):
    """分析进度事件"""
    type: str  # 事件类型
    data: Dict[str, Any]  # 事件数据


class AnalysisProgressData(BaseModel):
    """分析进度数据"""
    file_id: str
    step: str  # 当前步骤
    progress: int  # 分析进度 (0-100)
    message: str  # 进度消息
    step_progress: int  # 当前步骤进度


class AnalysisCompleteData(BaseModel):
    """分析完成数据"""
    file_id: str
    analysis_result: Dict[str, Any]  # 分析结果


class FileListRequest(BaseModel):
    """文件列表请求"""
    page: int = 1
    page_size: int = 10
    status: Optional[str] = None  # 文件状态筛选
    knowledge_base: Optional[str] = None  # 知识库筛选
    file_type: Optional[str] = None  # 文件类型筛选


class FileListItem(BaseModel):
    """文件列表项"""
    file_id: str
    file_name: str
    file_size: int
    file_type: str
    knowledge_base: Optional[str]
    status: str
    upload_time: datetime
    created_at: datetime

    class Config:
        from_attributes = True


class FileListResponse(BaseModel):
    """文件列表响应"""
    code: int = 200
    message: str = "获取文件列表成功"
    data: Optional[Dict[str, Any]] = None


class FileListData(BaseModel):
    """文件列表数据"""
    total: int
    page: int
    page_size: int
    items: List[FileListItem]


class FileDetailResponse(BaseModel):
    """文件详情响应"""
    code: int = 200
    message: str = "获取文件详情成功"
    data: Optional[Dict[str, Any]] = None


class FileDetailData(BaseModel):
    """文件详情数据"""
    file_id: str
    file_name: str
    file_size: int
    file_path: str
    file_hash: str
    file_type: str
    knowledge_base: Optional[str]
    status: str
    upload_progress: int
    upload_time: datetime
    created_at: datetime
    updated_at: datetime
    analysis_results: List[Dict[str, Any]] = []

    class Config:
        from_attributes = True


class FileDeleteResponse(BaseModel):
    """文件删除响应"""
    code: int = 200
    message: str = "文件删除成功"
    data: Optional[Dict[str, Any]] = None


# 错误码定义
ERROR_CODES = {
    400: "请求参数错误",
    401: "未授权访问",
    403: "权限不足",
    404: "文件不存在",
    413: "文件过大",
    415: "文件格式不支持",
    500: "服务器内部错误",
    1001: "文件上传失败",
    1002: "文件分析失败",
    1003: "文件格式不支持",
    1004: "文件大小超限",
    1005: "文件已存在"
} 